<?php
include("include/config.inc.php");
if(!isset($_SESSION['s_activIdsAdmin']))
{
	header("Location:checkLogin.php");
}
else
{
	if(isset($_POST['submit']) && ($_POST['stageProcessId'] > 0) && ($_POST['invoiceId'] > 0))
	{
		$stageProcessId  = isset($_POST['stageProcessId']) ? $_POST['stageProcessId'] : "";
		$invoiceId       = isset($_POST['invoiceId']) ? $_POST['invoiceId'] : "";
		$userId          = isset($_POST['userId']) ? $_POST['userId'] : 0;
		
		$insertContact = "INSERT INTO stagecontract (stageProcessId,invoiceId,userId)
		                       VALUES('".$stageProcessId."','".$invoiceId."',".$userId.")";
		$insertContactRes = mysql_query($insertContact);
		if(!$insertContactRes)
		{
			die("Insert Query Not Insertd :" .mysql_error());
		}
		else
		{
			header("Location:stageProcess.php");
		}
	}
  
	if(isset($_POST['submitCheck']) && ($_POST['checkedStageIds'] > 0))
	{
		for($c=0;$c<count($_POST['checkedStageIds']);$c++)
		{
		  $updateCheckContact = "UPDATE stagecontract
		                            SET action = 'C'
		                          WHERE stagecontractId = ".$_POST['checkedStageIds'][$c];
		  $updateCheckContactRes = mysql_query($updateCheckContact);
		  if(!$updateCheckContactRes)
		  {
		  	die("Update Query Not Insertd :" .mysql_error());
		  }
		  else
		  {
		  	header("Location:stageProcess.php");
		  }
	  }
	}  
  $processArray = array();
  $i=0;
  $selectprocess = "SELECT stageProcessId,stageProcessName
  	                 FROM stageprocess";
  $selectprocessRes = mysql_query($selectprocess);
  while($processRow = mysql_fetch_array($selectprocessRes))
  {
  	$processArray['stageProcessId'][$i]   = $processRow['stageProcessId'];
  	$processArray['stageProcessName'][$i] = $processRow['stageProcessName'];
  	$i++;
  }

  $userArray = array();
  $j=0;
  $selectUser = "SELECT userId,company
  	                      FROM user
  	                 WHERE userType = 'user'";
  $selectUserRes = mysql_query($selectUser);
  while($userRow = mysql_fetch_array($selectUserRes))
  {
  	$userArray['userId'][$j]  = $userRow['userId'];
  	$userArray['company'][$j] = $userRow['company'];
  	$j++;
  }
  
  $processArrayList = array();
  $k=0;
  $selectprocessList = "SELECT stagecontract.stagecontractId,stagecontract.action,user.company,
                               stageprocess.stageProcessName,invoice.invoiceNo
  	                      FROM stagecontract
  	                    LEFT JOIN stageprocess ON stageprocess.stageProcessId = stagecontract.stageProcessId 
  	                    LEFT JOIN invoice ON invoice.invoiceId = stagecontract.invoiceId
  	                    LEFT JOIN user ON user.userId = stagecontract.userId";
  $selectprocessListRes = mysql_query($selectprocessList);
  while($processListRow = mysql_fetch_array($selectprocessListRes))
  {
  	$processArrayList[$k]['stagecontractId']  = $processListRow['stagecontractId'];
  	$processArrayList[$k]['invoiceNo']        = $processListRow['invoiceNo'];
  	$processArrayList[$k]['action']           = $processListRow['action'];
  	$processArrayList[$k]['company']          = $processListRow['company'];
  	$processArrayList[$k]['stageProcessName'] = $processListRow['stageProcessName'];
  	$k++;
  }
  
  include("bottom.php");
  $smarty->assign('processArray',$processArray);
  $smarty->assign('processArrayList',$processArrayList);
  $smarty->assign('userArray',$userArray);
  $smarty->display("stageProcess.tpl");
}
?>